package game;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import server.Client;
import server.Data;

public class KeyClass implements KeyListener, Serializable {
	private Map map;
	private ObjectOutputStream out;
	private Player player;

	/**
	 * 
	 * @param g
	 */
	public KeyClass(Map map, ObjectOutputStream out) {
		this.map = map;
		this.out = out;
	}

	/**
	 * 
	 * @param ke
	 */
	
	public void addPlayer(Player player){
		this.player = player;
	}
	public void keyPressed(KeyEvent ke) {
		if (ke.getKeyCode() == KeyEvent.VK_UP) {
			map.playerMoved("up",player);
			Data tempData = new Data("up", player);
			try
			{
				out.writeObject(tempData);
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		if (ke.getKeyCode() == KeyEvent.VK_DOWN) {
			map.playerMoved("down",player);
			Data tempData = new Data("down", player);
			try
			{
				out.writeObject(tempData);
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
			map.playerMoved("left",player);
			Data tempData = new Data("left", player);
			try
			{
				out.writeObject(tempData);
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {
			map.playerMoved("right",player);
			Data tempData = new Data("right", player);
			try
			{
				out.writeObject(tempData);
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * 
	 * @param ke
	 */
	public void keyReleased(KeyEvent ke) {

	}

	/**
	 * @param ke
	 */
	public void keyTyped(KeyEvent ke) {

	}
}